using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace HR60_PanelSoft.FileUtils
{
	public class JsonPlus
	{
		private JavaScriptObject jobj = null;

		public JsonPlus (JavaScriptObject jobj)
		{
			this.jobj = jobj;
		}

		public Object getObject(string key)
		{
			if (!jobj.ContainsKey (key)) return null;
			return jobj [key];
		}

		public JsonPlus get(string key)
		{
			if (!jobj.ContainsKey (key)) return null;
			if (jobj [key] == null) return null;
			return new JsonPlus((JavaScriptObject) jobj [key]);
		}

		public String getString(string key)
		{
			object value = getObject(key);
			return value == null ? null : jobj [key].ToString();
		}

		public int getInt(string key)
		{
			object value = getObject(key);
			return value == null ? 0 : Int32.Parse(jobj [key].ToString());
		}

		public int getInt(string key, int defaultValue)
		{
			object value = getObject(key);
			return value == null ? defaultValue : Int32.Parse(jobj [key].ToString());
		}

		public bool getBoolean(string key)
		{
			String value = getString (key);
			return "True".Equals (value) || "1".Equals (value);
		}

		public JsonPlus[] getArray(string key)
		{
			if (!jobj.ContainsKey (key)) return null;

			object value = jobj [key];
			if (value == null) return null;
			JavaScriptArray jarray = (JavaScriptArray)value;

			JsonPlus[] jps = new JsonPlus[jarray.Count];
			for (int i=0; i<jarray.Count; i++)
			{
				jps [i] = new JsonPlus ((JavaScriptObject)jarray[i]);
			}
			return jps;
		}
	}
}

